package logic

import (
	"context"
	"fmt"
	"zhihu/zhihurpc/internal/svc"
	"zhihu/zhihurpc/models/mysql"
	"zhihu/zhihurpc/models/redis"
	"zhihu/zhihurpc/zhihurpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *zhihurpc.LoginRequest) (*zhihurpc.LoginResponse, error) {
	// todo: add your logic here and delete this line
	result, _ := redis.Rdb.Get(l.ctx, "sendSms"+in.Mobile).Result()
	if result != in.SendSms {
		return nil, fmt.Errorf("验证码错误,请稍后再试")
	}
	var user mysql.ZhihuUser
	mysql.DB.Debug().Where("mobile=?", in.Mobile).Find(&user)
	if user.ID == 0 {
		return nil, fmt.Errorf("手机号未被注册")
	}
	return &zhihurpc.LoginResponse{
		Id: int64(user.ID),
	}, nil
}
